package li.brauch.semesterarbeit.radio.impl;

import java.util.LinkedList;
import java.util.Queue;

import li.brauch.semesterarbeit.radio.AbstractRadio;
import li.brauch.semesterarbeit.radio.RadioPlugin;
import li.brauch.semesterarbeit.radio.model.StationTuneModel;
import li.brauch.semesterarbeit.radio.model.StationTuneResultModel;
import li.brauch.semesterarbeit.radio.model.Track;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;

public class LastFmRadioImpl extends AbstractRadio {
	
	private final String API_KEY = "057b4272f51477b97e99d831725c3674";
	private final String API_SEC = "d9ebba247e3e15dd7b12c93478545b39";
	
	private Session session;
	private Station station;
	
	private Queue<Track> playlist = new LinkedList<Track>();
	
	private Track currentTrack;
	
	public LastFmRadioImpl() {
		IPreferenceStore store = RadioPlugin.getDefault().getPreferenceStore();
		store.addPropertyChangeListener(new IPropertyChangeListener() {

			public void propertyChange(PropertyChangeEvent event) {
				
				// only properties of this implementation
				if (event.getProperty().startsWith("lastfm")) {
					// delegate event to plugin
					notifyRadioPropertyChanged();
				}
				
			}
		});
	}
	
	public StationTuneResultModel tuneStation(StationTuneModel model) throws Exception {
		
		// convert String to StationType
		StationType type = null;
		if (StationType.GLOBAL_TAG_RADIO.getLabel().equals(model.getType())) {
			type = StationType.GLOBAL_TAG_RADIO;
		} else if (StationType.SIMILAR_ARTISTS_RADIO.getLabel().equals(model.getType())) {
			type = StationType.SIMILAR_ARTISTS_RADIO;
		} else {
			type = StationType.TOP_FANS_RADIO;
		}
		
		station = Radio.tune(session, type, model.getName());
		
		StationTuneResultModel resultModel = new StationTuneResultModel();
		if (station != null)
			resultModel.setResults(new String[] {station.getName()});
		
		return resultModel;
	}

	public void banTrack() throws Exception {
		Radio.loveTrack(session, currentTrack);
	}

	public Track getNextTrack() throws Exception {
		
		if (playlist.size() == 0) {
			for (Track track : station.getPlaylist(session)) {
				playlist.add(track);
			}
		}
		
		// store current track for further operation
		currentTrack = playlist.poll();
		
		return currentTrack;
	}

	public String[] getStationTypes() {
		String[] stationTypes = new String[StationType.values().length];
		int i = 0;
		for (StationType stationType : StationType.values()) {
			stationTypes[i++] = stationType.getLabel();
		}
		return stationTypes;
	}

	public void login() throws Exception {
		String username = RadioPlugin.getDefault().getPreferenceStore().getString("lastfmusername");
		String password = RadioPlugin.getDefault().getPreferenceStore().getString("lastfmpassword");
		session = Radio.getMobileSession(username, password, API_KEY, API_SEC);
	}

	public void loveTrack() throws Exception {
		Radio.loveTrack(session, currentTrack);
	}

}
